package tchk.xyz.lingyuntutorminiprogram.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import tchk.xyz.lingyuntutorminiprogram.dto.WxLoginResponseDTO;
import tchk.xyz.lingyuntutorminiprogram.service.IUserService;
import tchk.xyz.lingyuntutorminiprogram.utils.Result;
import org.springframework.security.core.Authentication; // 导入这个

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/info")
    public Result<Object> getUserInfo() {
        // TODO: 从SecurityContextHolder获取当前登录用户信息并返回
        return Result.success("获取用户信息逻辑待实现");
    }

    /**
     * [新增] 用户选择或申请成为某个角色
     * @param roleCode 角色的编码 (e.g., 'PARENT' or 'TEACHER')
     * @param authentication Spring Security自动注入的当前用户信息
     * @return 更新后的用户信息和新的Token
     */
    @PostMapping("/select-role/{roleCode}")
    @PreAuthorize("isAuthenticated()")
    public Result<WxLoginResponseDTO> selectRole(@PathVariable String roleCode, Authentication authentication) {
        // 我们的旧代码里，参数是username，现在改为从Authentication里取openid，更安全
        String openid = authentication.getName();
        WxLoginResponseDTO response = userService.selectRole(openid, roleCode);
        return Result.success(response);
    }
}